﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace Enter.Common
{
    public class ListCtrlHelper
    {
        private static ListItem _allItem;

        public static ListItem AllItem
        {
            get
            {
                if (_allItem == null)
                {
                    _allItem = BuildDefaultAllListItem();
                }

                return _allItem;
            }
        }

        /// <summary>
        /// 构造一个值为0,显示为“全部”的选项
        /// </summary>
        /// <returns></returns>
        public static ListItem BuildDefaultAllListItem()
        {
            ListItem ls = new ListItem();
            ls.Selected = true;
            ls.Text = "--全部--";
            ls.Value = "0";
            return ls;
        }

        public static void BindEnumArrayOnListContrl(Array enumValues, ListControl listCtrl)
        {
            BindEnumArrayOnListContrl(enumValues, listCtrl, false);
        }

        public static void BindEnumValues<T>(ListControl listCtrl, IEnumerable<T> values)
        {
            if (!typeof(T).IsEnum) throw new ArgumentException(String.Format("{0}不是枚举类型", typeof(T).Name), "values");

            listCtrl.Items.Clear();

            foreach (Object enumValue in values)
            {
                ListItem item = new ListItem();
                item.Value = Convert.ToInt32(enumValue).ToString();
                item.Text = EnumHelper.GetEnumCNNameFromEnum(enumValue.GetType(), enumValue);
                listCtrl.Items.Add(item);
            }
        }

        public static void BindEnumArrayOnListContrl(Array enumValues, ListControl listCtrl, Boolean selectAll)
        {
            foreach (Object enumValue in enumValues)
            {
                ListItem item = new ListItem();
                item.Value = Convert.ToInt32(enumValue).ToString();
                item.Text = EnumHelper.GetEnumCNNameFromEnum(enumValue.GetType(), enumValue);
                item.Selected = selectAll;
                listCtrl.Items.Add(item);
            }
        }

        public static void BindEnumOnListControl(ListControl listCtrl, Type type, Int32 selectedValue)
        {
            if (!type.IsEnum)
                throw new ArgumentException("必须是枚举类型");

            Array statuses = Enum.GetValues(type);

            foreach (Object status in statuses)
            {
                ListItem item = new ListItem();
                int byteStatus = Convert.ToInt32(status);
                item.Value = byteStatus.ToString();
                item.Text = EnumHelper.GetEnumCNNameFromEnum(type, status);
                if (byteStatus == selectedValue)
                    item.Selected = true;

                listCtrl.Items.Add(item);
            }
        }

        public static void BindEnumOnListControl(ListControl listCtrl, Type type)
        {
            listCtrl.Items.Clear();
            BindEnumOnListControl(listCtrl, type, -1);
        }

        public static void BindEnumOnListControl(ListControl listCtrl, Type type, Boolean bindAll)
        {
            if (bindAll)
            {
                ListItem item = BuildDefaultAllListItem();
                item.Value = String.Join(",", EnumHelper.GetValues(type));
                listCtrl.Items.Add(item);
            }

            BindEnumOnListControl(listCtrl, type, -1);
        }

        public static void BindEnumOnListControlAndSelectAll(ListControl listCtrl, Type type)
        {
            if (!type.IsEnum)
                throw new ArgumentException("必须是枚举类型");

            Array statuses = Enum.GetValues(type);

            foreach (Object status in statuses)
            {
                ListItem item = new ListItem();
                int byteStatus = Convert.ToInt32(status);
                item.Value = byteStatus.ToString();
                item.Text = EnumHelper.GetEnumCNNameFromEnum(type, status);
                item.Selected = true;

                listCtrl.Items.Add(item);
            }
        }



        public static String[] GetSelectedValues(ListControl listCtrl)
        {
            List<String> values = new List<string>();

            foreach (ListItem item in listCtrl.Items)
            {
                if (item.Selected) values.Add(item.Value);
            }

            return values.ToArray();
        }
    }
}
